<?php

namespace XWAM\Addon\PXCallSystem\Model;

/**
 * DayBillModel数据库模型
 *
 * @author SoChishun <14507247@qq.com>
 * @version 1.0
 * @since 1.0.0 <2015-4-23> SoChishun Added.
 */
class DayBillModel extends AppbaseModel {

    /**
     * 数据表名称
     * @var string
     */
    protected $tableName = 'tcti_daybill';

    /**
     * 字段定义
     * @var array
     */
    protected $fields = array();

    /**
     * 命名范围
     * @var array
     */
    protected $_scope = array(
        'export' => array(
            'field' => "concat(siteID,'(',userName,')') as '租户ID', billdate as '日期', outAmount as '外呼', inAmount as '转座席', smsAmount as '短信', subtotal as '合计', rechargeAmount as '充值', blance as '余额'",
            'table' => 'vcti_daybill'
        ),
        'item_list' => array(
            'field' => 'id,siteID,billdate,outAmount,inAmount,smsAmount,subtotal,rechargeAmount,allowOverdraftAmount,blance',
            'order' => 'billdate desc',
        ),
    );

    /**
     * 是否批量验证
     * @var boolean
     */
    protected $patchValidate = true;

    /**
     * 验证规则
     * @var array
     */
    protected $_validate = array();

    /**
     * 自动填充
     * @var array
     */
    protected $_auto = array();

    /**
     * 返回管理员列表
     * @return type
     * @xince 1.0 <2015-6-9> SoChishun Added.
     */
    public function get_daybill_list($where) {
        $orderby = I('sort', $this->_scope['item_list']['order']) . ' ' . I('order');
        $page = I('page', 1) . ',' . I('rows', C('USER.PAGE_SIZE'));
        $total = $this->table('vcti_daybill')->where($where)->count('id');
        $list = $this->table('vcti_daybill')->where($where)->order($orderby)->page($page)->select();
        $sum = array(
            'outAmount' => $this->table('vcti_daybill')->where($where)->sum('outAmount'),
            'inAmount' => $this->table('vcti_daybill')->where($where)->sum('inAmount'),
            'smsAmount' => $this->table('vcti_daybill')->where($where)->sum('smsAmount'),
            'subtotal' => $this->table('vcti_daybill')->where($where)->sum('subtotal'),
            'rechargeAmount' => $this->table('vcti_daybill')->where($where)->sum('rechargeAmount'),
            'billdate' => '统计：',
        );
        return array('total' => $total, 'rows' => $list, 'footer' => array($sum));
    }

}
